package org.sunshinebox.team.sweettest;

import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;

public class DefaultInstanceManipulator implements InstanceManipulator {

	private final Object instance;
	private final Field[] fields;
	
	public DefaultInstanceManipulator(Object instance) {
		super();
		this.instance = instance;
		fields = instance.getClass().getFields();
	}

	public List<Field> getFieldAnnotatedWith(Class annotationClass) {
		List<Field> fieldList = new LinkedList<Field>();
		for (Field field : fields) {
			if (field.getAnnotation(annotationClass) != null)
				fieldList.add(field);
		}
		
		return fieldList;
	}

	public void setFieldValue(Field field, Object value) {
		try {
			field.set(instance, value);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	public Object getFieldValue(Field field) {
		try {
			return field.get(instance);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}

}
